package com.freexiaoyu.cloud.utils;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

/**
 * Created by DIY on 2018-11-20. 13:46
 * author:free宇
 * email:freexiaoyu@foxmail.com
 * describe:
 */

public class HanZiToPinYin {



    /**
     * 获得汉语拼音首字母
     *
     * @param chines 汉字
     * @return
     */
    public static String getAlpha(String chines) {
        String pinyinName = "";
        char[] nameChar = chines.toCharArray();
        HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
        defaultFormat.setCaseType(HanyuPinyinCaseType.UPPERCASE);
        defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        for (int i = 0; i < nameChar.length; i++) {
            if (nameChar[i] > 128) {
                try {
                    pinyinName += PinyinHelper.toHanyuPinyinStringArray(
                            nameChar[i], defaultFormat)[0].charAt(0);
                } catch (BadHanyuPinyinOutputFormatCombination e) {
                    e.printStackTrace();
                }
            } else {
                pinyinName += nameChar[i];
            }
        }
        return pinyinName;
    }

    /**
     * 将字符串中的中文转化为拼音,英文字符不变
     *
     * @param inputString 汉字
     * @return
     */
    public static String getPingYin(String inputString) {
        HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
        format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        format.setToneType(HanyuPinyinToneType.WITH_TONE_MARK);
        format.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE);
        String output = "";
        if (inputString != null && inputString.length() > 0
                && !"null".equals(inputString)) {
            char[] input = inputString.trim().toCharArray();
            try {
                for (int i = 0; i < input.length; i++) {
                    if (java.lang.Character.toString(input[i]).matches(
                            "[\\u4E00-\\u9FA5]+")) {
                        String[] temp = PinyinHelper.toHanyuPinyinStringArray(
                                input[i], format);
                        output +=" "+ temp[0];
                    } else
                        output +=java.lang.Character.toString(input[i]);
                }
            } catch (BadHanyuPinyinOutputFormatCombination e) {
                e.printStackTrace();
            }
        } else {
            return "*";
        }
        return output;
    }

    /**
     * 汉字转换位汉语拼音首字母，英文字符不变
     *
     * @param chines 汉字
     * @return 拼音
     */
    public static String converterToFirstSpell(String chines) {
        String pinyinName = "";
        char[] nameChar = chines.toCharArray();
        HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
        /**
         * WITH_V：用v表示ü (nv)
         WITH_U_AND_COLON：用"u:"表示ü (nu:)
         WITH_U_UNICODE：直接用ü (nü)
         * */
        defaultFormat.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE);
        /***
         * UPPERCASE：大写 (XING)
         LOWERCASE：小写 (xing)
         */
        defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        /***
         *
         * WITHOUT_TONE：无音标 (xing)
         WITH_TONE_NUMBER：1-4数字表示英标 (xing2)
         WITH_TONE_MARK：直接用音标符
         */
        defaultFormat.setToneType(HanyuPinyinToneType.WITH_TONE_MARK);
        for (int i = 0; i < nameChar.length; i++) {
            if (nameChar[i] > 128) {
                try {
                    pinyinName += PinyinHelper.toHanyuPinyinStringArray(
                            nameChar[i], defaultFormat)[0].charAt(0);
                } catch (BadHanyuPinyinOutputFormatCombination e) {
                    e.printStackTrace();
                }
            } else {
                pinyinName += nameChar[i];
            }
        }
        return pinyinName;
    }

    public static String getPinyinString(String hanzi) {
        if (hanzi != null && hanzi.length() > 0) {
            StringBuffer strbuf = new StringBuffer();
            String[] pinyin = new String[hanzi.length()];
            HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
            /**
             * WITH_V：用v表示ü (nv)
             WITH_U_AND_COLON：用"u:"表示ü (nu:)
             WITH_U_UNICODE：直接用ü (nü)
             * */
            format.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE);
            /***
             * UPPERCASE：大写 (XING)
             LOWERCASE：小写 (xing)
             */
            format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
            /***
             *
             * WITHOUT_TONE：无音标 (xing)
             WITH_TONE_NUMBER：1-4数字表示英标 (xing2)
             WITH_TONE_MARK：直接用音标符
             */
            format.setToneType(HanyuPinyinToneType.WITH_TONE_MARK);
            for (int index = 0; index < hanzi.length(); index++) {
                char c = hanzi.charAt(index);
                try {
                    String[] pinyinUnit = PinyinHelper.toHanyuPinyinStringArray(c, format);
                    if (pinyinUnit == null) {
                        pinyin[index] = "null";  // 非汉字字符，如标点符号
                        continue;
                    } else {
                        pinyin[index] = pinyinUnit[0]+" ";  // 带音调且长度固定为7个字符长度,,拼音居中,末尾优先
                    }
                } catch (BadHanyuPinyinOutputFormatCombination badHanyuPinyinOutputFormatCombination) {
                    badHanyuPinyinOutputFormatCombination.printStackTrace();
                }
                strbuf.append(pinyin[index]);
            }
            return strbuf.toString();
        } else {
            return null;
        }
    }

    // 每个拼音单元长度以7个字符长度为标准,拼音居中,末尾优先
    private static String formatCenterUnit(String unit) {
        String result = unit;
        switch(unit.length()) {
            case 1:
                result = "   " + result + "   ";
                break;
            case 2:
                result = "  " + result + "   ";
                break;
            case 3:
                result = "  " + result + "  ";
                break;
            case 4:
                result = " " + result + "  ";
                break;
            case 5:
                result = " " + result + " ";
                break;
            case 6:
                result = result + " ";
                break;
        }
        return result;
    }
}
